﻿Namespace PL.CRMSDK
    Public Class Project_PL
        Inherits PLBase
        Implements IPLProcess


        Public Property SequenceGeneration As Short = 1
        Private _aQueryName As String

        Public Sub New()
            MyBase.New()
            SequenceGeneration = 1
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
            _aQueryName = queryName
            SequenceGeneration = 1
        End Sub

        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
            If e.Command.CommandText = "bsp_Project_PL_Save" Then
                Me.MetaDataTable.Adapter.InsertCommand.Parameters("@GenerateProjectSEQ").Value = SequenceGeneration
                '
                Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@GenerateProjectSEQ").Value = SequenceGeneration
            End If
        End Sub

        Public Overrides Sub ValidateRow(row As DataRow)
            row.ClearErrors()
            'Mandatory Fields
            'TODO: Cache MetaString in AppManager, so here we read it once maybe for nothing
            'Dim ms As MetaString
            'ms = MetaString.GetMetaStringByCode("Mandatory", AppManager.ApplicationOption.LanguageId)
            If IsDBNull(row("CustomerId_FK")) Then
                row.SetColumnError("CustomerId_FK", MetaString.GetMetaStringByCode("MES-000007", AppManager.ApplicationOption.LanguageId).DefaultText) ' "Mandatory"
            End If

            If IsDBNull(row("ProjectBeginDate")) Then
                row.SetColumnError("ProjectBeginDate", MetaString.GetMetaStringByCode("MES-000007", AppManager.ApplicationOption.LanguageId).DefaultText)
            End If
            '
            If row.HasErrors Then
                row.RowError = MetaString.GetMetaStringByCode("MES-000007", AppManager.ApplicationOption.LanguageId).DefaultText ' "Please check your columns, a validation error occur"
            End If
        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return True
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Shared Sub CopyActivity(ActivityId As Guid, newName As String)
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("fspCopyProject")
                '
                sqlcmd.Parameters("@SourceProjectID").Value = ActivityId
                'sqlcmd.Parameters("@ProjectName").Value = newName
                '
                sqlcmd.ExecuteNonQuery()
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Catch ex As Exception
                Throw ex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Sub



        Public Function BatchRecords(dataRow() As DataRow) As Boolean Implements IPLProcess.BatchRecords

            Return True
        End Function

        Public Function CurrentRecord(dataRow As DataRow) As Boolean Implements IPLProcess.CurrentRecord

            Return True
        End Function
        ''' <summary>
        ''' NewRecord 
        ''' </summary>
        ''' <param name="dataRow"></param>
        ''' <returns>Boolean</returns>
        ''' <remarks>NewRecord  </remarks>
        Public Function NewRecord(dataRow As DataRow) As Boolean Implements IPLProcess.NewRecord

            dataRow("ProjectId") = System.Guid.NewGuid
            dataRow("ProjectBeginDate") = Now
            dataRow("ProjectEndDate") = Now

            'Howto: set Message by metaStringCode 
            Dim aMetaString As MetaString = MetaString.GetMetaStringByCode("", AppManager.LanguageId)
            If aMetaString IsNot Nothing Then
                dataRow("ProjectDescription") = String.Format("....{0}", aMetaString.DefaultText)
            End If
            aMetaString = MetaString.GetMetaStringByCode("", AppManager.LanguageId)
            If aMetaString IsNot Nothing Then
                dataRow("ProjectName") = String.Format("....{0}", aMetaString.DefaultText)
            End If

            Return True
        End Function
    End Class
End Namespace
